trigger AddRoutPlanEvent on RoutingPlan__c (after insert, after update, after delete) 
{
    List<RoutingPlan__c> rpList=Trigger.new;  
     if(trigger.isInsert)  
    {    
      //获取ownerId
      //RoutingPlan__c rId=[select RoutingPlanAndVisit__r.OwnerId from RoutingPlan__c where Id=:Trigger.new[0].Id];
      List<Id> idList=new List<Id>();
      for(RoutingPlan__c rp:Trigger.new)
      {
        idList.add(rp.Id);
      }
      List<RoutingPlan__c> rplanList=[select RoutingPlanAndVisit__r.OwnerId from RoutingPlan__c where Id in: idList];
      Map<Id,Id> idMapList=new Map<Id,Id>();
      for(RoutingPlan__c r:rplanList)
      {
        idMapList.put(r.Id,r.RoutingPlanAndVisit__r.OwnerId);
      }
      
      List<Event> InsertList=new List<Event>();
      for(RoutingPlan__c rp:Trigger.new)   
       {     
             if(idMapList.containsKey(rp.Id))
             {
                 Event e=new Event();     
                 e.Subject='计划'; 
                 e.WhatId=rp.Id;
                 e.StartDateTime=rp.StartDateTime__c;
                 e.EndDateTime=rp.EndDateTime__c; 
                 e.ReminderDateTime=rp.StartDateTime__c.addMinutes(-20); 
                 e.OwnerId=idMapList.get(rp.Id);
                 InsertList.add(e);
             }
                
        }  
        insert InsertList;
    } 
    if(trigger.isUpdate)
    {  
        List<Event> eventL =new List<Event>();
        List<Event> eList=[select Subject,WhatId,StartDateTime,EndDateTime from  Event where whatId in: Trigger.oldMap.keySet()];
         
        for(Integer i=0;i<eList.size();i++)
        {
            for(Integer j=0;j<Trigger.new.size();j++)
            {
                if(eList[i].WhatId==Trigger.new[j].Id)
                {
                    //eList[i].Subject=Trigger.new[j].VisitOutletName__c;  
                    eList[i].WhatId=Trigger.new[j].Id;
                    eList[i].StartDateTime=Trigger.new[j].StartDateTime__c; 
                    eList[i].EndDateTime=Trigger.new[j].EndDateTime__c; 
                    eList[i].ReminderDateTime=Trigger.new[j].StartDateTime__c.addMinutes(-20); 
                    eventL.add(eList[i]);
                }
            }
            
        }
        update eventL;
          
        /*Id ownerId = trigger.old[0].CreatedById;
        if(Trigger.old!=NULL)
        {
            for(integer i=0;i<Trigger.old.size();i++)  
            {
               
                List<Event> eventList=[select Subject,StartDateTime,EndDateTime from Event where StartDateTime=:trigger.old[i].StartDateTime__c and EndDateTime=:trigger.old[i].EndDateTime__c and Subject=:trigger.old[i].VisitOutletName__c and OwnerId = :ownerId];
                if(eventList!=NULL)
                {  
                   for(Event e:eventList) 
                  {
                      e.Subject=Trigger.new[i].VisitOutletName__c;  
                    e.StartDateTime=Trigger.new[i].StartDateTime__c; 
                    e.EndDateTime=Trigger.new[i].EndDateTime__c; 
                    e.ReminderDateTime=Trigger.new[i].StartDateTime__c.addMinutes(-20); 
                    eList.add(e);
                  }
               }
           }
            
        } */
        // if(filter.length()>0)
        //    sql+=' where '+filter;
        
        //trigger.old[0].StartDateTime__c.format('YYYY-MM-DDThh:mm:ssZ');
      
        //   trigger.new[0].StartDateTime__c.addError(sql);
        //   List<Event> evList=DataBase.query(sql);
        
        
    }  
    if(trigger.isDelete) 
    {
        List<Event> eList=new List<Event>();
        eList=[select Subject,WhatId,StartDateTime,EndDateTime from  Event where whatId in: Trigger.oldMap.keySet()];
        delete eList;
       /* if(trigger.old.size() > 0)
        {
           /* Datetime startTime, endTime,startTimeTem ;
            startTimeTem = trigger.old[0].StartDateTime__c.date();
            startTime=DateTime.newInstance(startTimeTem.Year(),startTimeTem .Month(),startTimeTem.Day(),8,0,1);
            endTime = startTime.addhours(15);
            Id ownerId = trigger.old[0].CreatedById;
           for(Integer i=0;i<trigger.old.size();i++)
           {                        
                List<Event> eventsToDelete = [select Id,Subject,StartDateTime,EndDateTime from Event where StartDateTime =:trigger.old[i].StartDateTime__c and EndDateTime =:trigger.old[i].EndDateTime__c and Subject=:trigger.old[i].VisitOutletName__c and OwnerId = :ownerId];
                if (eventsToDelete <> null) 
                {
                    for(Event e:eventsToDelete )
                      eList.add(e); 
                }
           } 
           delete eList;
        }  */
    }
}